package com.longrise.android.byjk.plugins.hra.hratabfirst;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/5.
 * HRA服务点详情页面--每天可预约名额页面
 */

public class HraPlacesFragment extends BaseFragment<HraPlacesPresenter> implements HraPlacesContract.View, HraPlacesAdapter.OnDetailItemClickListener {

    public static final String HRAPLACEFAGMENT = "orderparentbean";
    public static final String TAG = "HraPlacesFragment";
    private BBswipeRefreshLayout mSrl;
    private RecyclerView mRcv;
    private HraPlacesAdapter mAdapter;
    private OrderPlaceParentBean mOrderPlaceParentBean;
    private String bespeakdate;
    private String newDate;
    private String storeid;
//    private String machineid;
    private boolean mIsCreated;
    private List<OrderPlaceDefaultItem> mOrderDefaultItemList = new ArrayList<>();

    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_hra_places;
    }

    @Override
    public void initView() {
        mSrl = (BBswipeRefreshLayout) mRootView.findViewById(R.id.hraplace_fragment_swipelayout);
        mRcv = (RecyclerView) mRootView.findViewById(R.id.hraplace_fragment_irv);
        getExtraData();
        initRcv();
        initEvent();
        mIsCreated = true;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mIsCreated) {
            if (isVisibleToUser) {
                mPresenter.getTimeslotData(storeid, bespeakdate);
            } else {
                mAdapter.refreshData(getDefatutData());
            }
        }
    }

    public void getExtraData() {
        Bundle arguments = getArguments();
        mOrderPlaceParentBean = (OrderPlaceParentBean) arguments.getSerializable(HRAPLACEFAGMENT);
        bespeakdate = mOrderPlaceParentBean.getBespeakdate();  //请求库存时需要的时间
        newDate = mOrderPlaceParentBean.getNewDate();     //tab上显示的时间
        storeid = mOrderPlaceParentBean.getStoreid(); //服务点id
//        machineid = mOrderPlaceParentBean.getMachineid(); //机型id
    }

    private void initRcv() {
        mAdapter = new HraPlacesAdapter(getDefatutData());
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayout.HORIZONTAL);
        mRcv.setLayoutManager(manager);
        mRcv.setAdapter(mAdapter);
    }


    /**
     * 数据还没加载出来的时候 显示的假数据
     */
    private List<OrderPlaceDefaultItem> getDefatutData() {
        mOrderDefaultItemList.clear();
        for (int x = 0; x < 1; x++) {
            OrderPlaceDefaultItem item = new OrderPlaceDefaultItem(OrderPlaceDefaultItem.TYPE1);
            mOrderDefaultItemList.add(item);
        }
        return mOrderDefaultItemList;
    }

    public void initEvent() {
        mAdapter.setOnHraServiceItemClickListener(this);
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void onClick(OrderPlaceChildBean item, int position) {
        String timeslot = item.getTimeslot();
        String timeslotid = item.getTimeslotid();
        String machineid = item.getMachineid();
        String bespeakdate = item.getBespeakdate();
        String machineType = item.getMachineType();
        String price = item.getPrice();
        String machinemodel = item.getMachinemodel();
        HraServiceEvent event = new HraServiceEvent();
        event.setSelected(true);
        event.setPosition(position);
        event.setTimeslot(timeslot);
        event.setOrderDate(newDate);
        event.setTimeslotid(timeslotid);
        event.setMachineId(machineid);
        event.setBespeakdate(bespeakdate);
        event.setMachineType(machineType);
        event.setMachinModel(machinemodel);
        event.setPrice(price);
        EventBus.getDefault().post(event);
    }

    @Override
    public void refreshStockData(List<OrderPlaceDefaultItem> mParentBeanList) {

        mAdapter.refreshData(mParentBeanList);
    }
}
